package com.xyf.sys.service.impl;

import com.xyf.sys.config.CustomException;
import com.xyf.sys.domain.*;
import com.xyf.sys.mapper.RegimentAssistMapper;
import com.xyf.sys.service.*;
import com.xyf.sys.vo.AssistListRequestVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author R-H
 * @Date 2024/6/141437
 * @Description
 */
@Service
public class RegimentAssistServiceImpl implements IRegimentAssistService {

    @Autowired
    private RegimentAssistMapper mapper;
    @Autowired
    private IUserService userService;
    @Autowired
    private IWechatService wechatService;
    @Autowired
    private IRegimentAssistItemService assistItemService;
    @Autowired
    private IUserRoleService userRoleService;

    @Override
    public RegimentAssist get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void create(RegimentAssist assist) {
        User user = userService.get(assist.getUserId());
        String regimentId = user.getRegimentId();
        List<Role> roleList = userRoleService.getRoleList(user.getId());
        boolean merchantUser = roleList.stream().filter(role -> "MERCHANT".equalsIgnoreCase(role.getRoleKey())).findAny().isPresent();
        if (StringUtils.isBlank(regimentId) && !merchantUser){
            throw new CustomException("请先选择服务团长。");
        }
        assist.setType(1);
        if (merchantUser){
        }
//        wechatService.sendLaundryTemplateMessage();
        assist.setRegimentId(regimentId);
        if (assist.getId() == null){
            this.insert(assist);
        } else {
            this.update(assist);
        }
    }

    @Override
    public List<RegimentAssist> getList(AssistListRequestVo requestVo) {
        List<RegimentAssist> list = mapper.selectList(requestVo);
        for (RegimentAssist assist : list) {
            List<RegimentAssistItem> assistItems = assistItemService.getList(assist.getId());
            assist.setAssistList(assistItems);
        }
        return list;
    }

    @Override
    public List<RegimentAssist> exploreList(AssistListRequestVo requestVo) {
        requestVo.setStatus(0);
        List<RegimentAssist> list = mapper.selectExploreList(requestVo);
        for (RegimentAssist assist : list) {
            List<RegimentAssistItem> assistItems = assistItemService.getList(assist.getId());
            assist.setAssistList(assistItems);
        }
        return list;
    }

    @Override
    public void catchAssist(RegimentAssist body) {
        if (body.getId() == null){
            throw new CustomException("参数缺失");
        }
        RegimentAssist assist = this.get(body.getId());
        User user = userService.get(assist.getUserId());
        assist.setPrice(body.getPrice());
        assist.setRegimentId(user.getId());
        assist.setStatus("1");
        this.update(assist);
    }

    @Override
    public void insert(RegimentAssist assist) {
        assist.setCreateBy(assist.getUserId());
        assist.setCreateTime(new Date());
        assist.setStatus("0");
        assist.setDelFlag("0");
        assist.setLastUpdateTime(new Date());
        mapper.insert(assist);
    }

    @Override
    public void update(RegimentAssist assist) {
        assist.setUpdateBy(assist.getUserId());
        assist.setUpdateTime(new Date());
        assist.setLastUpdateTime(new Date());
        mapper.updateByPrimaryKey(assist);
    }

    @Override
    public void fulfill(RegimentAssist body) {
        RegimentAssist assist = this.get(body.getId());
        assist.setStatus("2");
        this.update(assist);
    }

    @Override
    public void addAssistItem(RegimentAssistItem assist) {
        RegimentAssistItem assistItem = assistItemService.get(assist.getAssistId(), assist.getUserId());
        if (assistItem == null){
            assist.setTimes(1);
            assist.setAssistDate(new Date());
            assist.setUserId(assist.getUserId());
            assist.setCreateBy(assist.getUserId());
            assistItemService.insert(assist);
        } else {
            assistItem.setTimes(assistItem.getTimes() + 1);
            assistItem.setAssistDate(new Date());
            assistItem.setUpdateBy(assist.getUserId());
            assistItem.setUpdateTime(new Date());
            assistItemService.update(assistItem);
        }
    }

    @Override
    public RegimentAssist getInfo(Long id) {
        RegimentAssist assist = this.get(id);
        if (assist == null){
            throw new CustomException("参数错误");
        }
        List<RegimentAssistItem> items = assistItemService.getList(assist.getId());
        assist.setAssistList(items);
        User user = userService.get(assist.getUserId());
        assist.setUserName(user.getName());
        assist.setUserMobile(user.getMobile());
        assist.setUserAvatar(user.getAvatar());
        User regiment = userService.get(assist.getRegimentId());
                assist.setRegimentName(regiment.getName());
        assist.setRegimentMobile(regiment.getMobile());
        assist.setRegimentAvatar(regiment.getAvatar());
        return assist;
    }

    @Override
    public void remove(Long id, String userId) {
        RegimentAssist assist = this.get(id);
        if (assist == null){
            throw new CustomException("参数错误");
        }
        assist.setDelFlag("1");
        assist.setUpdateBy(userId);
        assist.setUpdateTime(new Date());
        this.update(assist);
    }
}
